/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package digitaldreamers.libraries.games.clients.gameclient.gameui.implementation;

import digitaldreamers.libraries.games.clients.gameclient.gameui.abstracts.IScreen;
import digitaldreamers.libraries.games.clients.gameclient.gameui.abstracts.IScreenManager;
import java.awt.EventQueue;

/**
 *
 * @author DaoLe
 */
public abstract class ScreenManager implements IScreenManager {
    
    private static final String SCREEN_IS_NULL_ERROR_MESSAGE = "Screen must not be null.";
    
    protected WindowsGame m_Game = null;
    protected RenderPanel m_RenderPanel = null;
    protected IScreen m_CurrentScreen = null;
    
    void setRenderPanel(RenderPanel renderPanel) {
        m_Game = renderPanel.getGame();
        m_RenderPanel = renderPanel;
    }
    
    @Override
    public IScreen getCurrentScreen() {
        return m_CurrentScreen;
    }
    
    @Override
    public void setScreen(IScreen screen) {
        if (screen == null) {
            throw new IllegalArgumentException(SCREEN_IS_NULL_ERROR_MESSAGE);
        }
        if(m_CurrentScreen != null) {
            m_CurrentScreen.pause();
            m_CurrentScreen.dispose();
        }
        screen.resume();
        screen.update(0);
        m_RenderPanel.setScreen((GameScreen)screen);
        m_CurrentScreen = screen;
    }
    
    @Override
    public void update(long lDeltaTime) {
        if(m_CurrentScreen != null) {
            m_CurrentScreen.update(lDeltaTime);
        }
    }
    
    @Override
    public void present(long lDeltaTime) {
        if(m_CurrentScreen != null) {
            m_CurrentScreen.present(lDeltaTime);
        }
    }
    
    @Override
    public void start() {
        setScreen(getStartScreen());
    }
    
    @Override
    public void pause() {
        if(m_CurrentScreen != null) {
            m_CurrentScreen.pause();
        }
    }

    @Override
    public void resume() {
        if(m_CurrentScreen != null) {
            m_CurrentScreen.resume();
        }
    }
    
    @Override
    public void dispose() {
        if(m_CurrentScreen != null) {
            m_CurrentScreen.dispose();
        }
    }
}
